<!--
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-02-21 16:57:55
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-03-08 14:50:00
 * @FilePath: \hrManage\src\views\employee\index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryValue.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="search" />
        <el-tree
          ref="tree"
          node-key="id"
          :data="data"
          :props="defaultProps"
          :default-expand-all="true"
          :expand-on-click-node="false"
          :highlight-current="true"
          @node-click="selectedKey"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-permission="'adda'" size="mini" type="primary" @click="$router.push('employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <el-table :data="list">
          <el-table-column prop="staffPhoto" label="头像" align="center">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :size="30" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" />
          <el-table-column prop="workNumber" label="工号" />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>其他</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-row>
                <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
                <el-button size="mini" type="text" @click="role(row.id)">角色</el-button>
                <el-popconfirm
                  title="是否删除该员工信息？"
                  @onConfirm="delEmployee(row.id)"
                >
                  <el-button slot="reference" type="text">删除</el-button>
                </el-popconfirm>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" align="middle" style="height:60px">
          <el-pagination
            layout="total, prev, pager, next"
            :total="total"
            :page-size="queryValue.pagesize"
            :current-page="queryValue.page"
            @current-change="changePage"
          />
        </el-row>
      </div>
      <ImportExcel :show-excel-dialog.sync="showExcelDialog" @uploadSucess="getEmployee" />
      <RoleListVue :role-ids="roleIds" :show-role-list.sync="employeeRoleShow" :role-id="roleId" />
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getDepartmentTree } from '@/utils/index'
import { getEmployee, exportEmployee, delEmployee, getEmployeeDetail } from '@/api/employee'
import fileSave from 'file-saver'
import ImportExcel from './components/ImportExcel.vue'
import { getEnabled } from '@/api/role'
import RoleListVue from './components/RoleList.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel,
    RoleListVue
  },
  data() {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryValue: {
        page: 1,
        pagesize: 5,
        keyword: '',
        departmentId: null
      },
      list: [],
      total: 0,
      showExcelDialog: false,
      employeeRole: [],
      employeeRoleShow: false,
      roleIds: [],
      roleId: ''
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      this.data = getDepartmentTree(await getDepartment(), 0)
      this.queryValue.departmentId = this.data[0].id
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(this.queryValue.departmentId)
      })
      this.getEmployee()
    },
    selectedKey(node) {
      this.queryValue.departmentId = node.id
      this.queryValue.page = 1
      this.getEmployee()
    },
    async getEmployee() {
      const { rows, total } = await getEmployee(this.queryValue)
      this.list = rows
      this.total = total
    },
    changePage(newpage) {
      this.queryValue.page = newpage
      this.getEmployee()
    },
    search() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryValue.page = 1
        this.getEmployee()
      }, 500)
    },
    async exportEmployee() {
      const blob = await exportEmployee()
      fileSave.saveAs(blob, '员工信息表.xlsx')
    },
    async delEmployee(id) {
      await delEmployee(id)
      if (this.list.length === 1) {
        this.queryValue.page--
      }
      this.getEmployee()
      this.$message.success('删除成功')
    },
    async getEmpRole() {
      this.employeeRole = await getEnabled()
      console.log(this.employeeRole)
    },
    async role(id) {
      this.roleId = `${id}`
      this.roleIds = (await getEmployeeDetail(id)).roleIds
      this.employeeRoleShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
